package com.yuyu.qingmangmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.Query;
import com.yuyu.qingmangmall.product.dao.AttrAttrgroupRelationDao;
import com.yuyu.qingmangmall.product.dao.AttrGroupDao;
import com.yuyu.qingmangmall.product.entity.AttrAttrgroupRelationEntity;
import com.yuyu.qingmangmall.product.entity.AttrEntity;
import com.yuyu.qingmangmall.product.entity.AttrGroupEntity;
import com.yuyu.qingmangmall.product.service.AttrGroupService;
import com.yuyu.qingmangmall.product.service.AttrService;
import com.yuyu.qingmangmall.product.vo.AttrGroupRelationVo;
import com.yuyu.qingmangmall.product.vo.AttrgropAndAttrsVo;
import com.yuyu.qingmangmall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrGroupRelationDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        // select * from pms_attr_group where catelog_id = ? and (attr_group_id=key or attr_group_name like '%key%');
        if (!StringUtils.isNullOrEmpty(key)) { // 判断传入的key 在数据库中是否存在
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id",catelogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] relationVo) {

        List<AttrGroupRelationVo> attrGroupRelationVos = Arrays.asList(relationVo);

        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVos.stream().map((attrGroupRelationVo) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrGroupRelationVo, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        attrGroupRelationDao.deleteBatchRelation(collect);

    }

    /**
     * 根据分类Id查出所有分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrgropAndAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 拿到属性的所有分组
        List<AttrGroupEntity> attrGroupEntitys = this
                .list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        // 拿到分组对应的所有属性
        List<AttrgropAndAttrsVo> collect = attrGroupEntitys.stream().map(attrGroupEntity -> {
            AttrgropAndAttrsVo attrgropAndAttrsVo = new AttrgropAndAttrsVo();

            BeanUtils.copyProperties(attrGroupEntity, attrgropAndAttrsVo);
            // 通过分组Id拿到所有属性值
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
            attrgropAndAttrsVo.setAttrs(relationAttr);
            return attrgropAndAttrsVo;
        }).collect(Collectors.toList());
        //


        return collect;
    }

    @Override
    public List<SkuItemVo.SkuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1.查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SkuItemVo.SkuItemAttrGroupVo> attrGroupVos = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return attrGroupVos;
    }


}